import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Proudce } from '../Schema/produce.schema'
import { Model } from 'mongoose'
@Injectable()
export class ProduceService {
    constructor(@InjectModel(Proudce.name) private ProduceModel: Model<Proudce>) {
    }
    async gettypelist() {
        return await this.ProduceModel.find().populate('types')
    }
    addpro(req, res, body) {
        this.ProduceModel.create(body)
       
        res.send({
            code: 200,
            msg: '添加成功'
        })
    }
    async editpro(req, res, body) {
        await this.ProduceModel.updateOne({ _id: req.body._id }, req.body)
        res.send({
            code: 200,
            msg: '修改成功'
        })
    }
    async delpro(req, res) {
        await this.ProduceModel.deleteOne({ _id: req.body._id })
        res.send({
            code: 200,
            msg: '删除成功'
        })
    }
    async getProList(req, res) {
        const { name, shopname, state, value } = req.body;
        // 初始化查询对象
        const query: { name?: string, shopname?: any, state?: string, types?: any } = {};
        // 根据传的条件构建查询对象
        if (name) {
            query.name = name
        }
        if (shopname) {
            query.shopname = { $regex: new RegExp(shopname, 'gi') };
        }
        if (state) {
            query.state = state;
        }
        if (value) {
            query.types = value;
        }
        // 查询数据并返回
        const data = await this.ProduceModel.find(query).populate('types')
        res.send({
            code: 200,
            data: data
        });
    }
}
